﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using QRCoder;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace Course02.MVC.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class WeiController : ControllerBase
    {
        /// <summary>
        /// 
        /// </summary>
        [HttpPost]
        public IActionResult WeiM() 
        {
            string strCode = "http://47.96.28.125/Demo/Demo2/NineCJ.html";
            QRCodeGenerator qrGenerator = new QRCoder.QRCodeGenerator();
            QRCodeData qrCodeData = qrGenerator.CreateQrCode(strCode, QRCodeGenerator.ECCLevel.Q);
            QRCode qrcode = new QRCode(qrCodeData);

            //// qrcode.GetGraphic 方法可参考最下发“补充说明”
            Bitmap qrCodeImage = qrcode.GetGraphic(5, Color.Black, Color.White, null, 15, 6, false);
            MemoryStream ms = new MemoryStream();
            qrCodeImage.Save(ms, ImageFormat.Jpeg);

            //// 如果想保存图片 可使用  qrCodeImage.Save(filePath);

            //// 响应类型
            //context.Response.ContentType = "image/Jpeg";
            ////输出字符流
            //context.Response.BinaryWrite(ms.ToArray());
    
            byte[] s = new byte[ms.Length];
            ms.Position = 0;
            ms.Read(s, 0, s.Length);
            return File(s, "application/octet-stream", "aaa.jpg");
            //Response.ContentType = "application/octet-stream";
            //Response.Body = ms;
            //Response.BodyWriter
        }
    }
}
